﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Saker.Kernel.Model;
using Saker.Kernel.BLL;

namespace Saker.Web.Boss.Sys.Role
{
    public partial class RoleAdd : System.Web.UI.Page
    {
        string cmd, id;
        protected void Page_Load(object sender, EventArgs e)
        {
            cmd = Request["cmd"];
            id = Request["roleID"];
            if (!IsPostBack)
            {
                if (cmd == "Edit")
                {
                    bindData();
                }
            }
        }

        private void bindData()
        {
            SysRole role = new SysRoleBLL().GetByRoleID(int.Parse(id));
            txtRoleName.Text = role.RoleName;
            txtRoleDescription.Text = role.RoleDescription;
        }
        protected void lbtnReturn_Click(object sender, EventArgs e)
        {
            Response.Redirect("RolesList.aspx");
        }

        protected void lbtnSave_Click(object sender, EventArgs e)
        {
            SysRole role;
            if (cmd == "Add")
            {
                role = new SysRole();
            }
            else
            {
                role = new SysRoleBLL().GetByRoleID(int.Parse(id));
            }
            role.RoleName = txtRoleName.Text;
            role.RoleDescription = txtRoleDescription.Text;
            SysRoleBLL rolebll = new SysRoleBLL();
            object obj;
            if (cmd == "Edit")
            {
                obj = rolebll.Update(role);
                if (Convert.ToInt32(obj) <= 0)
                {
                    ScriptManager.RegisterStartupScript((Page)this, this.Page.GetType(), "alert", "$(function () { top.jBox.tip('保存失败！', 'error'); });", true);
                }
                else
                {
                    ScriptManager.RegisterStartupScript((Page)this, this.Page.GetType(), "alert", "$(function () { top.jBox.tip('保存成功！', 'success'); });", true);
                }
            }
            else
            {
                obj = rolebll.Add(role);
                if (null == obj)
                {
                    ScriptManager.RegisterStartupScript((Page)this, this.Page.GetType(), "alert", "$(function () { top.jBox.tip('保存失败！', 'error'); });", true);
                }
                else
                {
                    ScriptManager.RegisterStartupScript((Page)this, this.Page.GetType(), "alert", "$(function () { top.jBox.tip('保存成功！', 'success'); });", true);
                }
            }
        }
    }
}